Component({
    options: {
        styleIsolation: 'apply-shared'
    },
    properties: {
        imgHost: {
            type: String
        },
        data: {
            type: Object
        },
        index: { type: Number },
        tab_active: { type: Number },
        supplier_type: Number,
    },
    methods: {
        //修改库存
        inputBlurForSku(e) {
            this.prohibitToDetail()
            let { value } = e.detail
            if (value === '') return wx.showToast({
                title: '请输入库存数量',
                icon: 'none'
            })

            const { data, index } = this.data
            Number(value) !== Number(data.sku) && this.triggerEvent('editSku', {
                index,
                product_id: data.product_id,
                sku: value
            })

        },
        //修改普通价格
        inputBlurForPrice(e) {
            this.prohibitToDetail()
            const { data, index } = this.data

            let value = this.formatter(e.detail.value)
            value = value ? value : ''
            this.triggerEvent('setCostPrice', {
                index,
                cost_price: value
            })

            if (value === '') return wx.showToast({
                title: '请输入价格',
                icon: 'none'
            })
            if (this.judgePrice()) {
                value !== Number(data.cost_price) && this.triggerEvent('editPrice', {
                    index,
                    product_id: data.product_id,
                    cost_price: value
                })
            }
        },
        //修改批发价格
        inputBlurForWholesalePrice(e) {
            this.prohibitToDetail()
            const { data, index } = this.data

            let value = this.formatter(e.detail.value)
            value = value ? value : ''
            this.triggerEvent('setWholesalePrice', {
                index,
                wholesalePrice: value
            })

            if (value === '') return wx.showToast({
                title: '请输入价格',
                icon: 'none'
            })
            if (this.judgePrice()) {
                value !== Number(data.purchase_price) && this.triggerEvent('editWholesalePrice', {
                    index,
                    product_id: data.product_id,
                    wholesalePrice: value
                })
            }
        },
        /**验证金额输入 */
        formatter(value) {
            value = value.toString()
            value = value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
            value = value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
            value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
            value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数
            if (value.indexOf(".") < 0 && value != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
                value = parseFloat(value);
            }
            value = parseFloat(value)
            if (Number.isNaN(value)) return ''
            return value
        },
        //判断批发价格不能少于普通供应价格
        judgePrice() {
            const cost_price = Number(this.data.data.cost_price)
            const wholesale_price = Number(this.data.data.purchase_price)
            if (wholesale_price > cost_price) {
                wx.showToast({
                    title: '批发供应价不能大于普通供应价',
                    icon: 'none',
                    duration: 3000
                })
                return false
            } else {
                return true
            }
        },
        prohibitToDetail() {
            this.triggerEvent('prohibitToDetail')
        }
    }
})